package com.yoyo.yyadsdk.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SpUtil {

    private static SharedPreferences sp;

    private static SpUtil instance;

    public static void init(Context context) {
        sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 对外唯一实例的接口
     */
    public static SpUtil getInstance() {
        if (null == instance) {
            // 同步处理避免同时生成多个实例
            synchronized (SpUtil.class) {
                if (null == instance) {
                    instance = new SpUtil();
                }
            }
        }
        return instance;
    }

    private void setSp(String key, String value) {
        if (null != sp) {
            sp.edit().putString(key, value).apply();
        }
    }

    private static final String FILL_NAME = "config";

    private static final String CITY = "city";

    public static void setCity(Context context, String city) {
        SharedPreferences sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE);
        sp.edit().putString(CITY, city).apply();
    }

    public static String getCity(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE);
        return sp.getString(CITY, null);
    }

    private static final String COUNTRY = "country";

    public static void setCountry(Context context, String country) {
        SharedPreferences sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE);
        sp.edit().putString(COUNTRY, country).apply();
    }

    public static String getCountry(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILL_NAME, Context.MODE_PRIVATE);
        return sp.getString(COUNTRY, null);
    }

    private static final String AD_PERIOD = "adPeriod";

    public static void setAdPeriod(long adPeriod) {
        sp.edit().putLong(AD_PERIOD, adPeriod).apply();
    }

    public static long getAdPeriod() {
        return sp.getLong(AD_PERIOD, TimeUtils.DAY);
    }

    private static final String AD_FREQUENCY_ID = "adFrequencyId";

    public static void setAdFrequencyId(long adFrequencyId) {
        sp.edit().putLong(AD_FREQUENCY_ID, adFrequencyId).apply();
    }

    public static long getAdFrequencyId() {
        long frequencyId = sp.getLong(AD_FREQUENCY_ID, 0L);
        long currentTimeMillis = System.currentTimeMillis();
        if (frequencyId > 0) {
            long period = getAdPeriod() + frequencyId;
            if (currentTimeMillis > period) {
                setAdFrequencyId(period);
                return period;
            }
            return frequencyId;
        } else {
            setAdFrequencyId(currentTimeMillis);
            return currentTimeMillis;
        }
    }

//    private static final String AD_FREQUENCY_POSITION = "adFrequencyPosition";
//
//    public static void setAdFrequencyPosition(int position) {
//        sp.edit().putInt(AD_FREQUENCY_POSITION, position).apply();
//    }
//
//    public static int getAdFrequencyPosition() {
//        return sp.getInt(AD_FREQUENCY_POSITION, 0);
//    }

    private static final String LOCK_ADV_MAX_COUNT = "lockAdvMaxCount";

    public static void setLockAdvMaxCount(int lockAdvMaxCount) {
        sp.edit().putInt(LOCK_ADV_MAX_COUNT, lockAdvMaxCount).apply();
    }

    public static int getLockAdvMaxCount() {
        return sp.getInt(LOCK_ADV_MAX_COUNT, 2);
    }

    private static final String RESET_RUNNING_APPS = "resetRunningApps";

    public static void setResetRunningApps(long lockAdvMaxCount) {
        sp.edit().putLong(RESET_RUNNING_APPS, lockAdvMaxCount).apply();
    }

    public static boolean getResetRunningApps() {
        long time = sp.getLong(RESET_RUNNING_APPS, 0L);
        if (time > 0) {
            boolean b = System.currentTimeMillis() - time > 4 * TimeUtils.HOUR;
            if (b) {
                setResetRunningApps(System.currentTimeMillis());
            }
            return b;
        } else {
            setResetRunningApps(System.currentTimeMillis());
            return true;
        }
    }

    private static final String HAS_REPORT_BASE_DATA = "hasReportBaseData";

    public static void setHasReportBaseData() {
        sp.edit().putLong(HAS_REPORT_BASE_DATA, System.currentTimeMillis()).apply();
    }

    public static boolean getHasReportBaseData() {
        long aLong = sp.getLong(HAS_REPORT_BASE_DATA, 0L);
        if (aLong > 0L) {
            return System.currentTimeMillis() - aLong > 2 * TimeUtils.HOUR;
        } else
            return true;
    }

    private static final String CONFIG_ENABLE = "configEnable";

    public static void setConfigEnable(boolean configEnable) {
        sp.edit().putBoolean(CONFIG_ENABLE, configEnable).apply();
    }

    public static boolean getConfigEnable() {
        return sp.getBoolean(CONFIG_ENABLE, false);
    }

}
